package liumengbaio.lolzhangyoubao.Activity.Adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;

import liumengbaio.lolzhangyoubao.Activity.Activity.MoreNovelBodyActivity;
import liumengbaio.lolzhangyoubao.Activity.InFo.MoreNovelDetailsInfo;
import liumengbaio.lolzhangyoubao.R;

/**
 * 小说详情页面 适配器
 * Created by lanouhn on 16/9/6.
 */
public class MoreNovelDetailsAdapter extends RecyclerView.Adapter {
    private Context mContext;
    private MoreNovelDetailsInfo mMoreNovelDetailsInfo;

    public MoreNovelDetailsAdapter(Context mContext, MoreNovelDetailsInfo mMoreNovelDetailsInfo) {
        this.mContext = mContext;
        this.mMoreNovelDetailsInfo = mMoreNovelDetailsInfo;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_more_noveldetails_body, null);
        holder = new BodyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        BodyViewHolder bodyViewHolder = (BodyViewHolder) holder;


        bodyViewHolder.title.setText(mMoreNovelDetailsInfo.getData().get(position).getTitle());
        bodyViewHolder.content.setText(mMoreNovelDetailsInfo.getData().get(position).getDesc());

        Long data = mMoreNovelDetailsInfo.getData().get(position).getPublished() * 1000L;
        Date date = new Date(data);
        SimpleDateFormat format = new SimpleDateFormat("MM-dd hh:mm");
        String time = format.format(date);
        bodyViewHolder.update.setText(time);

        bodyViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String item_id = mMoreNovelDetailsInfo.getData().get(position).getItem_id();
                String url = "http://api.lol.zhangyoubao.com/mobiles/item/" + item_id + "?v_=400901&token=1285c08b6" +
                        "12411d0bfc35f213eb5d92a73&user_id=15863305&size=middle&t=1473127793";
                Intent intent = new Intent(mContext, MoreNovelBodyActivity.class);
                intent.putExtra("url", url);
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mMoreNovelDetailsInfo.getData().size();
    }

    private class BodyViewHolder extends RecyclerView.ViewHolder {
        private TextView title, update, content;

        public BodyViewHolder(View itemView) {
            super(itemView);
            title = (TextView) itemView.findViewById(R.id.more_novel_details_title);
            update = (TextView) itemView.findViewById(R.id.more_novel_details_time);
            content = (TextView) itemView.findViewById(R.id.more_novel_details_content);
        }
    }
}
